package com.fxsh168.server.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class BalanceMoneyActivity extends BaseActivity implements View.OnClickListener {

    private EditText mCashDrawalamount;
    private ImageView mBtnCashDrawalToAlipay, mBtnCashDrawalToWeixin;
    private String mMoney;
    private int type = 1;
    private float intMoney;
    PopupWindows_tixian popupWindows_tixian;
    LinearLayout liWeiXin, liZhiFuBao;
    TextView teCash;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance_cash_withdrawal);
        mCashDrawalamount = (EditText) findViewById(R.id.cashDrawalamount);
        teCash = (TextView) findViewById(R.id.teCash);
        liWeiXin = (LinearLayout) findViewById(R.id.liWeiXin);
        liZhiFuBao = (LinearLayout) findViewById(R.id.liZhiFuBao);
        mBtnCashDrawalToAlipay = (ImageView) findViewById(R.id.btnCashDrawalToAlipay);
        mBtnCashDrawalToWeixin = (ImageView) findViewById(R.id.btnCashDrawalToWeixin);
        teCash.setOnClickListener(this);
        liWeiXin.setOnClickListener(this);
        liZhiFuBao.setOnClickListener(this);
        Intent intent = getIntent();
        String action = intent.getAction();
        if (action != null && !TextUtils.isEmpty(action)) {
            if (action.equals("intMoney")) {
                intMoney = intent.getFloatExtra("intMoney", 0);
            }
        }
    }


    //设置选择的体现方式
    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.liZhiFuBao:
                type = 1;
                mBtnCashDrawalToAlipay.setImageResource(R.mipmap.gou);
                mBtnCashDrawalToWeixin.setImageResource(R.mipmap.heigou);
                break;
            case R.id.liWeiXin:
                type = 2;
                mBtnCashDrawalToAlipay.setImageResource(R.mipmap.heigou);
                mBtnCashDrawalToWeixin.setImageResource(R.mipmap.gou);
                break;
            case R.id.teCash:
                mMoney = mCashDrawalamount.getText().toString().trim();
                if (mMoney.isEmpty()) {
                    showToast("提现金额不能为空");
                } else if (Integer.parseInt(mMoney) < 100) {
                    showToast("提现金额不能低于100");
                } else {
                    btnCashDrawal(view);
                }

                break;
        }
    }


    public void btnCashDrawal(final View view) {
        //参数：phone 手机号
        //token
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        String mPhone = sp.getString("mPhone", "");
        String token = sp.getString("token", "");
        float intMoney = sp.getFloat("intMoney", 0);
        //获取提取金额
        mMoney = mCashDrawalamount.getText().toString().trim();
        float moneyInt = Float.parseFloat(mMoney);
//        int moneyInt=(int)d;
        if (mMoney != null && !TextUtils.isEmpty(mMoney) && (moneyInt <= intMoney && moneyInt > 0)) {
            OkHttpUtils.post().addParams("balance", mMoney)
                    .addParams("phone", mPhone)
                    .addParams("token", token)
                    .url(Constant.SIJITIXIAN1)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            showToast("连接不上服务器!");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Log.e("ebtnCashDrawal", "response==" + response);
                            if (response != null) {
                                RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                                String code = regiYZMCG.getCode().toString().trim();
                                if (code.equals(200 + "")) {
                                    popupWindows_tixian = new PopupWindows_tixian(BalanceMoneyActivity.this, view);
                                } else if (code.equals(201 + "")) {
                                    showToast("非法操作!");
                                } else if (code.equals(202 + "")) {
                                    showToast("提现失败!");
                                } else if (code.equals(203 + "")) {
                                    showToast("未绑定账号!");
                                } else if (code.equals(205 + "")) {
                                    showToast("已提交申请，请不要重复提交!");
                                } else {
                                    showToast("提现失败!");
                                }
                            }
                        }
                    });
        } else {
            showToast("现金余额不足!");
        }

    }

    //          提现提示
    public class PopupWindows_tixian extends PopupWindow {
        View view;

        public PopupWindows_tixian(Context mContext, View parent) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.popwin_tixian, null);
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            update();
            TextView te_ok_konw = (TextView) view.findViewById(R.id.te_ok_konw);
            te_ok_konw.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindows_tixian.dismiss();
                }
            });
        }
    }

}
